package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 用户地址功能实现类
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的UserAddress表mapper对象
     */
    @Autowired
    private UserAddressMapper _UserAddressMpper;

 			
			

    /**
     * 用户地址分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<UserAddressDto> List(UserAddressPagedInput input) {

      
        //声明一个支持用户地址查询的(拉姆达)表达式
        LambdaQueryWrapper<UserAddress> queryWrapper = Wrappers.<UserAddress>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,UserAddress::getId,input.getId())
                .eq(input.getCreatorId()!=null,UserAddress::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入Privoce不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPrivoce())) {
            queryWrapper=queryWrapper.eq(UserAddress::getPrivoce,input.getPrivoce());
       	 }
     	 //如果前端搜索传入City不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getCity())) {
            queryWrapper=queryWrapper.eq(UserAddress::getCity,input.getCity());
       	 }
     	 //如果前端搜索传入Zoom不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getZoom())) {
            queryWrapper=queryWrapper.eq(UserAddress::getZoom,input.getZoom());
       	 }
     	 //如果前端搜索传入Address不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getAddress())) {
            queryWrapper=queryWrapper.eq(UserAddress::getAddress,input.getAddress());
       	 }
     	 //如果前端搜索传入LinkName不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getLinkName())) {
            queryWrapper=queryWrapper.eq(UserAddress::getLinkName,input.getLinkName());
       	 }
     	 //如果前端搜索传入Phone不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPhone())) {
            queryWrapper=queryWrapper.eq(UserAddress::getPhone,input.getPhone());
       	 }
     	 //如果前端搜索传入UserId不为空,则进行精确查询
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(UserAddress::getUserId,input.getUserId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(UserAddress::getCreationTime);
        //构建一个分页查询的model
        Page<UserAddress> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取用户地址数据
        IPage<UserAddress> pageRecords= _UserAddressMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _UserAddressMpper.selectCount(queryWrapper);

        //把UserAddress实体转换成UserAddress传输模型
        List<UserAddressDto> items= Extension.copyBeanList(pageRecords.getRecords(),UserAddressDto.class);
        for (UserAddressDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto UserDTO = new AppUserDto();        
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
           if(UserEntity!=null) {  
          BeanUtils.copyProperties(UserDTO,UserEntity);
            item.setUserDto(UserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个用户地址查询
     */
    @SneakyThrows
    @Override
    public UserAddressDto Get(UserAddressPagedInput input) {
       if(input.getId()==null)
        {
            return new UserAddressDto();
        }
        PagedResult<UserAddressDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new UserAddressDto();
    }

    /**
     *用户地址创建或者修改
     */
    @SneakyThrows
    @Override
    public UserAddressDto CreateOrEdit(UserAddressDto input) {

        //声明一个用户地址实体
        UserAddress UserAddress = new UserAddress();

        //把前端传入的input参数拷贝到用户地址实体
        BeanUtils.copyProperties(UserAddress,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(UserAddress);

        //定义一个返回给前端的用户地址传输模型
        UserAddressDto UserAddressDto=new UserAddressDto();

        //同理把操作的用户地址实体拷贝给用户地址传输模型
        BeanUtils.copyProperties(UserAddressDto,UserAddress);

        //把传输模型返回给前端
        return UserAddressDto;
    }



}
